#!/usr/bin/env python

import sys

if len(sys.argv) < 2:
    print "Usage: test2.py infile [infiles]"

def avg(xs):
    total = 0.0
    for x in xs:
        total += x
    return total/len(xs)

def prune(xs):
    xs.sort()
    return xs[5:-5]

class CountingSet(object):
    def __init__(self):
        super(CountingSet, self).__init__()
        self._data = {}

    def add(self, x):
        if self._data.has_key(x):
            self._data[x] += 1
        else:
            self._data[x] = 1

    def __str__(self):
        str_repr = []
        for key in self._data.keys():
            str_repr.append("%s (%i)"%(key, self._data[key]))
        return ', '.join(str_repr)

for filename in sys.argv[1:]:
    with open(filename, 'r') as infile:
        lines = infile.readlines()

    overheads = []
    totals = []
    set = CountingSet()

    for line in lines:
        if '-1' in line:
            continue
        
        tokens = line.split(' ')
        scheduling_overhead, peer = tokens[:2]
        total_time = tokens[-1]

        
        overheads.append(float(scheduling_overhead))
        set.add(peer)
        totals.append(float(total_time))
        
    totals = prune(totals)
    overheads = prune(overheads)

    print "%s: overhead=%f (%f, %f), total=%f (%f, %f), peers=%s"%(filename, 
                                                                   avg(overheads), 
                                                                   min(overheads), 
                                                                   max(overheads), 
                                                                   avg(totals), 
                                                                   min(totals), 
                                                                   max(totals), 
                                                                   str(set))
        
